const util = require('../../utils/util.js');
//获取应用实例
const app = getApp();
Page({
	data: {
		imgpath:app.setting.apiimgpath,
		files:'../../images/95.png',
		upimg:'',
		ticketList:[],
		text:'tomfriwel',
		identifier:''
	},
	chooseImage:function(e){
		var that = this;
		wx.chooseImage({
			sizeType: ['original', 'compressed'],
			sourceType: ['album', 'camera'],
			success: function(res) {        
				wx.showToast({
					title:'图片加载中',
					icon:'loading'
				});
				that.setData({
					upimg:String(res.tempFilePaths),
					files:String(res.tempFilePaths)
				});
			}
		})
	},
	previewImage:function(e){
		wx.previewImage({
			current:e.currentTarget.id,
			urls:this.data.files
		})
	},
	nameInput:function(e){
		this.setData({
			identifier:e.detail.value
		})
	},
	previewPic:function(e){
		var src = e.currentTarget.dataset.src;
		var images = [src]
		//图片预览
		wx.previewImage({
		    current:src ,
		    urls:images
		})
	},
	//上传小票
	formSubmit:util.throttle(function(e){
		var that = this;
		if(!that.data.upimg){
			wx.showToast({
				title:'请上传小票',
				icon:'none',
			});
			return false;
		}
		if(!that.data.identifier){
			wx.showToast({
				title:'请输入商户编号/订单号',
				icon:'none',
			});
			return false;
		}
		wx.showLoading({
			title: '正在上传',
			mask: true
		});
		wx.uploadFile({
			url:app.setting.apiwebpath + '/api/Member/editorUploadImage',
			filePath:that.data.upimg,
			name:'file',
			formData:{openid:wx.getStorageSync('member_id')},
			headers:{'Content-Type':'application/json'},
			success(res) {
				var _data = JSON.parse(res.data);
				if(_data.code == 0){
					app.request('/api/Member/smallTicketSubmit', {pic:_data.data.filename,identifier:that.data.identifier}, function(res){
						if(res.code == '-1'){
							wx.showModal({
							    title:'提示',
							    content:res.msg,
							    showCancel:false,
							    success(res){
									if(res.confirm){
									   that.setData({
											files:'../../images/95.png',
											upimg:'',
											identifier:''
										})
									} 
							    }
							})
							return false;
						}
						that.onLoad();
						that.setData({
							files:'../../images/95.png',
							upimg:'',
							identifier:''
						})
					});
				}
			}

		})
	}, 1000),
	onLoad: function() {
		var that = this;
		//小票记录
		app.request('/api/Member/getSmallTicketList', {apid:1}, function(res){
			if(res.code == '0'){
				//更新数据
				that.setData({
					ticketList:res.data
				});
			}
		});
	}
});